﻿Imports Microsoft.Office.Interop.Word
Imports System.IO
Imports System.Collections.ObjectModel
Public Class Anexa5Class
    Inherits AnexaClass
    Public Sub New()
        Me.ShortName = "Anexa5"
    End Sub
    Private _Acte As New ObservableCollection(Of Act)
    Public Property Acte As ObservableCollection(Of Act)
        Get
            Return _Acte
        End Get
        Set(value As ObservableCollection(Of Act))
            _Acte = value
            OnPropertyChanged("Acte")
        End Set
    End Property
    Public Overrides Sub toWordOrPDF(Optional format As Microsoft.Office.Interop.Word.WdSaveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault, Optional printOnly As Boolean = False)
        Dim Word As New Application
        Dim AnexaDoc As New Document

        Dim sablon As Object
        sablon = (assemblyPath & "\WordTemplates\" & Me.ShortName & ".dotx")
        AnexaDoc = Word.Documents.Add(sablon, False, WdNewDocumentType.wdNewBlankDocument, False)
        Dim currentDirectory = SetariClass._LucruPath & "\" & Me.Autorizat.Nume & "\" & Me.OCPI.denumire & "\" & Me.Imobil.UAT.Denumire & "\" & Me.proprietar.Nume
        If IO.Directory.Exists(currentDirectory) = False Then
            IO.Directory.CreateDirectory(currentDirectory)
        End If

        FillText("OCPI", AnexaDoc, Me.OCPI.denumire)
        FillText("BCPI", AnexaDoc, Me.BCPI.denumire)
        FillText("NumeProprietar", AnexaDoc, Me.proprietar.Nume)
        Dim oneForPF As Integer = 2
        If Me.proprietar.IsPF Then oneForPF = 1
        FillList("OneForPF", AnexaDoc, oneForPF, False)
        FillText("AdresaProprietar", AnexaDoc, Me.proprietar.adresa.adresaIntreaga)
        If proprietar.IsPF = False Then DeleteCC("ProprietarIsPJ2bd", AnexaDoc, False)
        FillText("SerieCIProprietar", AnexaDoc, Me.proprietar.SerieCI)
        FillText("NrCIProprietar", AnexaDoc, Me.proprietar.NrCI)
        FillText("IDProprietar", AnexaDoc, Me.proprietar.id)
        FillText("NumeMandatar", AnexaDoc, Autorizat.Nume)
        FillText("AdresaImobil", AnexaDoc, Imobil.Adresa.adresaIntreaga)
        Dim listaActe As String = ""
        For i = 0 To Acte.Count - 1
            listaActe = listaActe + Acte(i).Denumire + ", "
            MsgBox(listaActe)
        Next
        listaActe = listaActe.Substring(0, listaActe.Length - 2)
        FillText("ActeProprietate", AnexaDoc, listaActe)

        Dim ext As String = ".docx"
        If format = WdSaveFormat.wdFormatPDF Then ext = ".pdf"
        If printOnly = True Then
            Try
                AnexaDoc.Activate()
                If File.Exists(currentDirectory & "\" & Me.ShortName & ".docx") = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                Else
                    File.Delete(currentDirectory & "\" & Me.ShortName & ".docx")
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                End If
                Word.PrintOut(FileName:=currentDirectory & "\" & Me.ShortName & ".docx")

            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source, MsgBoxStyle.Information)
            End Try
        Else
            Try
                If File.Exists(currentDirectory & "\" & Me.ShortName & "" & ext) = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & "" & ext, format)
                Else
                    If MsgBox("Fisierul exista! Doriti sa-l suprascrieti?", MsgBoxStyle.YesNo, "Sunteti sigur?") = MsgBoxResult.Yes Then
                        File.Delete(currentDirectory & "\" & Me.ShortName & ext)
                        AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ext, format)
                    Else
                        MsgBox("Niciun fisier nu a fost modificat", MsgBoxStyle.OkOnly)
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source + vbCrLf + ex.StackTrace, vbOKOnly)
            End Try
        End If

        AnexaDoc.Close(WdSaveOptions.wdDoNotSaveChanges)
        AnexaDoc = Nothing
        'AnexaDoc.SaveAs2(currentDirectory & "\ANEXA1.docx")
        'Word.Visible = True
        Word.Quit(WdSaveOptions.wdDoNotSaveChanges)
        Word = Nothing
    End Sub


End Class
